package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List> list(){
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    //请求网址: http://localhost:8080/shoppingCart/add
    //请求方法: POST
    @PostMapping("/add")
    public R<ShoppingCart> add(HttpServletRequest request, @RequestBody ShoppingCart shoppingCart){
        shoppingCart.setUserId((Long)request.getSession().getAttribute("user"));
        LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        //如果数组库里有这个菜，那么就+1
        List<ShoppingCart> shoppingCarts1 = shoppingCartService.list(queryWrapper1);
        if(shoppingCarts1.size()>0){
            ShoppingCart target = shoppingCarts1.get(0);
            BeanUtils.copyProperties(target,shoppingCart);
            shoppingCart.setNumber(target.getNumber()+1);
            shoppingCartService.updateById(shoppingCart);
            return R.success(shoppingCart);
        }

        LambdaQueryWrapper<ShoppingCart> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        //如果数组库里有这个套餐，那么就+1
        List<ShoppingCart> shoppingCarts2 = shoppingCartService.list(queryWrapper2);
        if(shoppingCarts2.size()>0){
            ShoppingCart target = shoppingCarts2.get(0);
            BeanUtils.copyProperties(target,shoppingCart);
            shoppingCart.setNumber(target.getNumber()+1);
            shoppingCartService.updateById(shoppingCart);
            return R.success(shoppingCart);
        }
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }

    //请求网址: http://localhost:8080/shoppingCart/sub
    //请求方法: POST
    @PostMapping("/sub")
    public R<String> sub(HttpServletRequest request,@RequestBody Map map){
        Long userId = (Long) request.getSession().getAttribute("user");
        String setmealId = (String)map.get("setmealId");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> setmealShoppingCarts = shoppingCartService.list(queryWrapper);
        if(setmealShoppingCarts!=null && setmealShoppingCarts.size()>0){
            ShoppingCart setmealShoppingCart = setmealShoppingCarts.get(0);
            if(setmealShoppingCart!=null){
                if(setmealShoppingCart.getNumber()>1){
                    setmealShoppingCart.setNumber(setmealShoppingCart.getNumber()-1);
                    shoppingCartService.updateById(setmealShoppingCart);
                }else {
                    log.info("删除套餐{}",setmealId);
                    shoppingCartService.removeById(setmealShoppingCart.getId());
                }

            }
        }



        String dishId = (String)map.get("dishId");
        LambdaQueryWrapper<ShoppingCart> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        dishQueryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> dishShoppingCarts = shoppingCartService.list(dishQueryWrapper);
        if(dishShoppingCarts!=null&&dishShoppingCarts.size()>0){
            ShoppingCart dishShoppingCart = dishShoppingCarts.get(0);
            if(dishShoppingCart!=null){
                if(dishShoppingCart.getNumber()>1){
                    dishShoppingCart.setNumber(dishShoppingCart.getNumber()-1);
                    shoppingCartService.updateById(dishShoppingCart);
                }else {
                    log.info("删除菜品{}",dishId);
                    shoppingCartService.removeById(dishShoppingCart.getId());
                }

            }

        }
        return R.success("");
    }

    //请求网址: http://localhost:8080/shoppingCart/clean
    //请求方法: DELETE
    @DeleteMapping("/clean")
    public R<String> clean(HttpServletRequest request){
        Long userId  = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功！");
    }
}
